package com.qyl.http_upload.demo;


import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URLEncoder;

public class TestDemo {
    private int port;
    private String host;
    private Socket socket;
    private BufferedReader bufferedReader;
    private BufferedWriter bufferedWriter;

    public TestDemo(String host, int port) {
        socket = new Socket();
        this.host = host;
        this.port = port;
    }

    public void sendGet() throws IOException {
        String path = "/file/definition?name=222";
//        Socket socket = new Socket(this.host, this.port);
//        socket.setSoTimeout(30000);
        SocketAddress dest = new InetSocketAddress(this.host, this.port);
        socket.connect(dest);
        OutputStreamWriter streamWriter = new OutputStreamWriter(socket.getOutputStream());
        bufferedWriter = new BufferedWriter(streamWriter);
//        StringBuffer get = new StringBuffer(200);
//        get.append("GET /file/definition?name=importsource HTTP/1.1\r\n");
//        get.append("Host: 127.0.0.1:8080\r\n");
//        get.append("Accept: text/html\r\n");
//        get.append("Connection: Close\r\n");
//        get.append("\r\n");
//        get.append("");
        bufferedWriter.write("GET " + path + " HTTP/1.1\r\n");
        bufferedWriter.write("Host: " + this.host + "\r\n");
        bufferedWriter.write("Connection: Close\r\n"); // 这句必须写，否则链接不会关闭
        bufferedWriter.write("\r\n");
        bufferedWriter.flush();

//        // socket客户端接收tomcat返回的数据
//        BufferedReader inReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//        // 以下是服务器返回的数据
//        System.out.println("**************** Server responsed ****************");
//        String tmp = "";
//        while ((tmp = inReader.readLine()) != null) {
//            // 解析服务器返回的数据，做相应的处理
//            System.out.println(tmp);
//        }
//        inReader.close();

        BufferedInputStream streamReader = new BufferedInputStream(socket.getInputStream());
        bufferedReader = new BufferedReader(new InputStreamReader(streamReader, "utf-8"));
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        bufferedReader.close();
        bufferedWriter.close();
        socket.close();
    }

    public void sendPost() throws IOException {
        String path = "/file/text";
        String data = URLEncoder.encode("name", "utf-8") + "=" + URLEncoder.encode("gloomyfish", "utf-8") + "&" +
                URLEncoder.encode("age", "utf-8") + "=" + URLEncoder.encode("32", "utf-8");
        // String data = "name=zhigang_jia";
        SocketAddress dest = new InetSocketAddress("127.0.0.1", 8080);
        socket.connect(dest);
        OutputStreamWriter streamWriter = new OutputStreamWriter(socket.getOutputStream(), "utf-8");
        bufferedWriter = new BufferedWriter(streamWriter);
        StringBuffer sb = new StringBuffer();
        sb.append("POST " + path + " HTTP/1.1\r\n");
        sb.append("Host: " + this.host + "\r\n");
        sb.append("Content-Length: " + data.length() + "\r\n");
        sb.append("Content-Type: application/x-www-form-urlencoded\r\n");
        sb.append("Connection: Close\r\n");
        sb.append("\r\n");
        sb.append(data);
        sb.append("\r\n");
        sb = new StringBuffer();
        sb.append(sendData());
        System.out.println(sb);

        System.out.println("........................");
        bufferedWriter.write(sb.toString());
        bufferedWriter.flush();

        BufferedInputStream streamReader = new BufferedInputStream(socket.getInputStream());
        bufferedReader = new BufferedReader(new InputStreamReader(streamReader, "utf-8"));
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        bufferedReader.close();
        bufferedWriter.close();
        socket.close();
    }


    // 发送报文
//    private String sendData(){
//        return """
//POST /file/text HTTP/1.1
//Host: 127.0.0.1
//Content-Length: 22
//Content-Type: application/x-www-form-urlencoded
//Connection: Close
//
//name=gloomyfish&age=32
// """;
//    }

    private String sendData(){
        return """
POST /file/text HTTP/1.1
Host: 127.0.0.1
Content-Type: multipart/form-data; boundary=ss
Content-Length: 113
Connection: Close

--ss
Content-Disposition: form-data; name="id"

111
--ss
Content-Disposition: form-data; name="name"

222
--ss--
""";
    }

    private static String s(){

        return """
--ss
Content-Disposition: form-data; name="id"

111
--ss
Content-Disposition: form-data; name="name"

222
--ss--
""";
    }


    public static void main(String[] args) {
        TestDemo td = new TestDemo("127.0.0.1", 8080);
        try {
//             td.sendGet(); //send HTTP GET Request
//            System.out.println(s().length());
            td.sendPost(); // send HTTP POST Request
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
